package com.example.myapplication.ic;

import android.content.Context;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.EditText;

import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest;
import com.example.myapplication.callback.LoginCallBack;

/**
 * Created by panyijun on 16/3/1.
 */
public class LoginInterfaceImpl implements ILoginInterface{
    @Override
    public void login(String id,String pass,final LoginCallBack listener) {
        RequestParams params = new RequestParams();
        params.addBodyParameter("TeacherNumber",id);
        params.addBodyParameter("TeacherPassword",pass);
        HttpUtils utils = new HttpUtils();
        utils.send(HttpRequest.HttpMethod.POST,
                "http://192.168.191.1/app/index.php/teacher/login", params,
                new RequestCallBack<String>() {
                    @Override
                    public void onLoading(long total, long current, boolean isUploading) {

                    }

                    @Override
                    public void onSuccess(ResponseInfo<String> responseInfo) {
                        listener.onSuccess(responseInfo.result);

                    }

                    @Override
                    public void onStart() {
                    }

                    @Override
                    public void onFailure(HttpException error, String msg) {
                        //// TODO: 2015/12/18  ∂
                        listener.onFail(msg);
                    }


                });





    }

    @Override
    public boolean judgeUserNameAndPassWord(Context context,EditText userName, EditText passWord) {
        // Reset errors.
        userName.setError(null);
        passWord.setError(null);

        // Store values at the time of the login attempt.
        String email = userName.getText().toString();
        String password = passWord.getText().toString();

        boolean cancel = false;
        View focusView = null;

        // Check for a valid password, if the user entered one.
        if (!TextUtils.isEmpty(password) && !isPasswordValid(password)) {
            passWord.setError("密码太短");
            focusView = passWord;
            cancel = true;
        }

        // Check for a valid email address.
        if (TextUtils.isEmpty(email)) {
            userName.setError("教师ID不能为空");
            focusView = userName;
            cancel = true;
        }
        // Check for a valid email address.
        if (TextUtils.isEmpty(password)) {
            passWord.setError("密码不能为空");
            focusView = userName;
            cancel = true;
        }
        if (cancel) {
            focusView.requestFocus();
        } else {
        }
        return cancel;
    }


    private boolean isPasswordValid(String password) {
        return password.length() > 4;
    }
}
